package com.geovis.csb.system.controller;


import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.geovis.common.core.api.Result;
import com.geovis.common.core.controller.BaseController;
import com.geovis.common.mybatis.page.PageParam;
import com.geovis.common.mybatis.page.PageResult;
import com.geovis.csb.system.entity.SystemApp;
import com.geovis.csb.system.service.ISystemAppService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 应用表
 * </p>
 *
 * @author zengds
 */
@RestController
@RequestMapping("/system/systemApp")
@RequiredArgsConstructor
@Api(value = "系统管理模块-应用信息相关接口", tags = "系统管理模块-应用信息相关接口")
@Slf4j
@Validated
public class SystemAppController extends BaseController<ISystemAppService> {

    @ApiOperation(value = "保存应用信息", notes = "保存应用信息")
    @PostMapping({"/save"})
    public Result save(@Validated @RequestBody SystemApp app) {
        baseService.save(app);
        return Result.ok();
    }

    @ApiOperation(value = "修改应用信息", notes = "修改资源信息")
    @PostMapping({"/update"})
    public Result update(@Validated @RequestBody SystemApp app) {
        baseService.updateById(app);
        return Result.ok();
    }

    @ApiOperation(value = "查询所有应用数据", notes = "查询所有应用数据")
    @PostMapping("/list")
    public Result<List<SystemApp>> list() {
        List<SystemApp> list = baseService.list(Wrappers.lambdaQuery(SystemApp.class).orderByAsc(SystemApp::getOrderNum));
        list.forEach(item -> {
            item.setParams(null);
        });
        return Result.ok(list);
    }

    @ApiOperation(value = "分页查询应用信息", notes = "分页查询应用信息")
    @PostMapping("/listPage")
    public Result<PageResult<SystemApp>> listPage(@RequestBody @Validated PageParam<SystemApp> pageParam) {
        IPage<SystemApp> page = pageParam.buildPage();
        LambdaQueryWrapper<SystemApp> queryWrapper = Wrappers.lambdaQuery(SystemApp.class);
        SystemApp queryDTO = pageParam.getQuery();
        if (ObjectUtil.isNotEmpty(queryDTO)) {
            queryWrapper.like(StrUtil.isNotEmpty(queryDTO.getName()), SystemApp::getName, queryDTO.getName());
        }
        baseService.page(page, queryWrapper);
        return Result.ok(new PageResult<SystemApp>(page));
    }

    @ApiOperation(value = "批量删除应用数据", notes = "批量删除应用数据")
    @ApiImplicitParams({@ApiImplicitParam(name = "idList", value = "删除id的list", required = true, dataType = "java.util.Set", paramType = "body")})
    @PostMapping("/removeByIdList")
    public Result removeByIdList(@NotNull(message = "删除的id集合不能为空") @RequestBody(required = false) Set<String> idList) {
        baseService.removeByIds(idList);
        return Result.ok();
    }

    @ApiOperation(value = "应用校验", notes = "应用校验")
    @GetMapping("/appValidator/{appId}")
    public Result appValidator(@PathVariable("appId") String appId) {
        SystemApp app = baseService.getById(appId);
        Assert.isFalse(ObjectUtil.isEmpty(app), "应用查找不到");
        return Result.ok(app.getParams());
    }

}
